//
//  DetailCommentViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailCommentViewController.h"
#import "DetailCommentModel.h"
#import "DetailCommentCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "DetailCommentVideoViewController.h"
#import "MJRefresh.h"


@interface DetailCommentViewController ()
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *detailArr;
@property(nonatomic,assign)NSInteger pageNum;

@end


@implementation DetailCommentViewController

- (void)dealloc
{
    [_tableView release];
    [_detailArr release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailArr = [NSMutableArray array];
        self.pageNum = 2;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self GetVideo];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, (VIEWHEIGHT  -49) *0.94585) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = (VIEWHEIGHT -64 -49) *0.18953;
    [self addHeader];
    [self addFooter];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    DetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[DetailCommentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    DetailCommentModel *comMo = self.detailArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:comMo.pic_url];
    [cell.picImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.titleLabel.text = comMo.title;
    cell.descLabel.text = comMo.desc;
     cell.selectionStyle = NO;
    return cell;
}

-(void)GetVideo
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/videos?catwordid=%@&page=1&i_=70CCD531-7601-4F9D-B1E8-130796BF8400&t_=1430186756&p_=22613&v_=40040306&d_=ios&osv_=8.3&version=0&a_=lol", self.id] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = dic[@"data"];
        for (NSMutableDictionary *dic in arr) {
            DetailCommentModel *comMo = [[DetailCommentModel alloc]init];
            [comMo setValuesForKeysWithDictionary:dic];
            [self.detailArr addObject:comMo];
            [comMo release];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

-(void)addHeader{
    [self.tableView headerBeginRefreshing];
    
    [self.tableView  addHeaderWithCallback:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
            [self.tableView reloadData];
            [self.tableView headerEndRefreshing];
        });
    }];
    
}

-(void)addFooter{
         [self.tableView addFooterWithCallback:^{
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 *  NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 
                 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                 [manager GET:[NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/videos?catwordid=%@&page=%ld&i_=70CCD531-7601-4F9D-B1E8-130796BF8400&t_=1430186756&p_=22613&v_=40040306&d_=ios&osv_=8.3&version=0&a_=lol", self.id, self.pageNum] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                     NSMutableDictionary *dic = responseObject;
                     NSMutableArray *arr = dic[@"data"];
                     for (NSMutableDictionary *dic in arr) {
                         DetailCommentModel *comMo = [[DetailCommentModel alloc]init];
                         [comMo setValuesForKeysWithDictionary:dic];
                         [self.detailArr addObject:comMo];
                         [comMo release];
                     }
                     self.pageNum++;
                     [self.tableView reloadData];
                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                     
                 }];
                  [self.tableView footerEndRefreshing];
             });

         }];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailCommentVideoViewController *comView = [[DetailCommentVideoViewController alloc]init];
    [self.navigationController pushViewController:comView animated:YES];
    [comView release];
    DetailCommentModel *comMo = self.detailArr[indexPath.row];
    comView.url = comMo.video_url;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
